﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class PatientBLL
    {
        private static int patientid = 0;
        HospitalDataContext DB = new HospitalDataContext();
        public List<Patient> SelectAll()
        {
            return DB.Patients.ToList();
        
        }
        public List<Patient> SelectDesc()
        {
            return DB.Patients.OrderByDescending(p => p.PatientID).ToList();
        }
        public List<Patient> SelectNew()
        {
            return DB.Patients.Where(p => p.Status == "in").OrderByDescending(p => p.PatientID).ToList();
        }
        public String Insert(PatientDTO giatri)
        {
            try
            {
                Patient patient = new Patient();
                patient.PatientID = (patientid++).ToString();
                patient.LocalDoctorID = giatri.Localdoctorid;
                patient.NOKID = giatri.Nokid;
                patient.FirstName = giatri.FirstName;
                patient.LastName = giatri.Lastname;
                patient.Address = giatri.Address;
                patient.Sex = giatri.Sex;
                patient.Tel = giatri.Tel;
                patient.DateOfBirth = giatri.Dob;
                patient.MaritalStatus = giatri.Maritalstatus;
                patient.DateRegistered = giatri.Dateregistered;
                patient.Status = giatri.Status;
                DB.Patients.InsertOnSubmit(patient);
                DB.SubmitChanges();
                return "success";
            }
            catch(Exception e)
            {
               return e.Message;
            }
        }
        //ham update
        public string Update(PatientDTO nhap)
        {
            try
            {
                Patient patient = DB.Patients.Where(pa => pa.PatientID==nhap.Patientid).FirstOrDefault();
                patient.LocalDoctorID = nhap.Localdoctorid;
                patient.NOKID = nhap.Nokid;
                patient.FirstName = nhap.FirstName;
                patient.LastName = nhap.Lastname;
                patient.Address = nhap.Address;
                patient.Sex = nhap.Sex;
                patient.Tel = nhap.Tel;
                patient.DateOfBirth = nhap.Dob;
                patient.MaritalStatus = nhap.Maritalstatus;
                patient.DateRegistered = nhap.Dateregistered;
                patient.Status = nhap.Status;
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        //Update thanh outpatient-------------------------
        public string UpdateTo_OutPatient(string patientid)
        {
            try
            {
                Patient patient = DB.Patients.Where(pa => pa.PatientID == patientid).FirstOrDefault();
                patient.Status = "outpa";
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception ee)
            {
                return ee.Message;
            }
        }
        //Update to inpatient-------------------------
        public string UpdateTo_InPatient(string patientid)
        {
            try
            {
                Patient patient = DB.Patients.Where(pa => pa.PatientID == patientid).FirstOrDefault();
                patient.Status = "inpa";
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception ee)
            {
                return ee.Message;
            }
        }
        //ham search theo ID va Name cua Patient
        public List<Patient> Search(string key)
        {
            return DB.Patients.Where(p => p.PatientID.Contains(key) || p.FirstName.Contains(key) || p.LastName.Contains(key)).ToList();
        }
        //ham Search theo Next Of Kin
        public IQueryable SearchWithNOK(string key)
        {
            return DB.Patients.Join(
                DB.NextOfKins,
                a => a.NOKID,
                b => b.NOKID,
                (a, b) => new
                {
                    PatientID = a.PatientID,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Address = a.Address,
                    Sex = a.Sex,
                    Tel = a.Tel,
                    DateRegistered = a.DateRegistered,
                    Status = a.Status,
                    NOKID = b.NOKID,
                    FullName = b.FullName
                }
                ).Where(p => p.NOKID.Contains(key) || p.FullName.Contains(key)).AsQueryable();
        }
        public IQueryable SearchWithLocalDoctor(string key)
        {
            return DB.Patients.Join(
                DB.LocalDoctors,
                a => a.LocalDoctorID,
                b => b.LocalDoctorID,
                (a, b) => new
                {
                    PatientID = a.PatientID,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Address = a.Address,
                    Sex = a.Sex,
                    Tel = a.Tel,
                    DateRegistered = a.DateRegistered,
                    Status = a.Status,
                    LocalDoctorID = b.LocalDoctorID,
                    FullName = b.FullName
                }
                ).Where(p => p.LocalDoctorID.Contains(key) || p.FullName.Contains(key)).AsQueryable();
        }
        public string Get_ID_From_listwaitid(string listwaitid)
        {
            var kqs = (from listw in DB.ListWaits
                      join appoint in DB.Appointments
                      on listw.AppointmentID equals appoint.AppointmentID into las
                      from la in las.DefaultIfEmpty()
                      join pa in DB.Patients
                      on la.PatientID equals pa.PatientID into lpas
                      from lpa in lpas.DefaultIfEmpty()
                      where listw.ListID == listwaitid
                      select new
                      {
                          lpa.PatientID
                      }
                   ).Distinct();
            var kq = kqs.FirstOrDefault();
            return kq.PatientID;
        }
        public IQueryable Search_PatientOnWaitList_withWardID(string wardid)
        {
            var kqs = (from newlistw in DB.ListWaits
                       join appoint in DB.Appointments
                       on newlistw.AppointmentID equals appoint.AppointmentID into las
                       from la in las.DefaultIfEmpty()
                       join pa in DB.Patients
                       on la.PatientID equals pa.PatientID into lpas
                       from lpa in lpas.DefaultIfEmpty()
                       where newlistw.WardID == wardid && newlistw.Status == "new"
                       select new
                       {
                           lpa.PatientID,
                           Name = lpa.FirstName + " " + lpa.LastName,
                           newlistw.DateRegistered,
                           lpa.Sex,
                           lpa.Tel,
                           lpa.Address,
                           newlistw.WardID,
                           newlistw.ListID

                       }
                   ).Distinct(); // (moi.ActualLeave==null || moi.ActualLeave > DateTime.Now) );
            Console.WriteLine("tim benh nhan hien tai" + kqs.Count());
            return kqs;

        }
    }
}
